<?php
session_start();
include_once("auth_process.php");
?>
<?php

function handleError($e)
{
    error_log("product_detail.php, Error: " . $e->getMessage());
    echo "Sorry, some errors happened. Please try later !";
    exit();
}

include_once("db_connection.php");

try {
    $conn = getdb();

    $stmt = $conn->prepare("SELECT pid, catid, pname, price, description,extension   FROM products WHERE pid = :pid");
    $stmt->bindParam(':pid', $_GET['pid']);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $current_product = $stmt->fetch();
} catch (Exception $e) {
    handleError($e);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="../css/common.css" rel="stylesheet" type="text/css"/>
    <link href="../css/nav_list.css" rel="stylesheet" type="text/css"/>
    <style>

        #nav_menu {
            display: inline-block;
        }

        #product_detail {
            padding-top: 30px;
        }

        p {
            font-size: 22px;
        }

        #item_pic {
            display: inline-block;
            zoom: 1;
            width: 360px;
            height: 360px;
            vertical-align: top;
        }

        #item_text {
            zoom: 1;
            display: inline-block;
            width: calc(100% - 690px);
            min-width: 200px;
            padding-left: 20px;
            border-left: solid 1px grey;
        }


        .item_name {
            text-align: left;
        }

        .item_price {
            text-align: left;
        }

        .item_description {

        }
    </style>
    <title>Freewheel Shopping Online</title>
</head>
<body>
<?php
$username = getCurrentUsername();
if ($username['result'] != true) {
    echo "<p  id='login_entry'><a href='login.php'>Hi, please login!</a></p>";
} else {
    echo "<p  id='login_entry'>Hello, " . htmlspecialchars($username['username']) . " | <a href='logout.php'>Log out</a></p>";
}
$cart_token = md5(time() . "cart_token" . rand(0, 9999));
$_SESSION['cart_token'] = $cart_token;
?>
<div id="container">
    <?php readfile("../htmls/header.html") ?>
    <div id="body">
        <nav id="category_list">
            <ul>
                <?php

                try {
                    $stmt = $conn->prepare("SELECT catid , cname  FROM categories");
                    $stmt->execute();

                    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    $categories = $stmt->fetchAll();
                    foreach ($categories as $k => $v) {
                        if ($current_product != null && $v['catid'] == $current_product['catid']) {
                            $current_category = $v;
                        }
                        echo "<li><a href=\"category_detail.php?catid=" . $v['catid'] . "\">" . htmlspecialchars($v['cname']) . "</a></li>\n";
                    }
                } catch (Exception $e) {
                    handleError($e);
                }

                ?>
            </ul>
        </nav>

        <nav id="nav_menu">
            <a href="index.php">Home</a> &gt;
            <?php
            if ($current_product != null) {
                echo "<a href=\"category_detail.php?catid=" . $current_category['catid'] . "\">" . htmlspecialchars($current_category['cname']) . "</a> &gt;\n
            <a href=\"product_detail.php?pid=" . $current_product['pid'] . "\">" . htmlspecialchars($current_product['pname']) . "</a>";
            }
            ?>

        </nav>

        <?php include_once("shopping_cart_form.php") ?>

        <section id="product_detail">

            <?php if ($current_product != null): ?>
                <img id="item_pic"
                     src="<?php echo "../images/products/product" . $current_product['pid'] . "." . htmlspecialchars($current_product['extension']) ?>"
                     alt="<?php echo htmlspecialchars($current_product['pname']) ?>">
                <div id="item_text">
                    <p class="item_name"> <?php echo htmlspecialchars($current_product['pname']) ?> </p>
                    <p class="item_price">Price: HK$ <?php echo htmlspecialchars($current_product['price']) ?> </p>
                    <input type="button" class="add_to_cart" value="Add To Cart"/>
                    <div class="item_description">
                        <h2>Product Description</h2>
                        <p><?php echo htmlspecialchars($current_product['description']) ?></p>
                    </div>
                </div>
            <?php else: ?>
                <p>Product Not Found</p>
            <?php endif; ?>

        </section>
    </div>
    <?php readfile("../htmls/footer.html") ?>
</div>
<script type="text/javascript" src="../javascripts/myLib.js"></script>
<script type="text/javascript" src="../javascripts/shopping_cart.js"></script>
<script>
    <?php
        echo "document.querySelector(\".add_to_cart\").onclick = addToCart({$current_product['pid']});";
    ?>
</script>

</body>
</html>
